#include "StdAfx.h"
#include "CSnithtButton.h"

IMPLEMENT_DYNAMIC(CSnightButton, CEdit)

CSnightButton::CSnightButton(void)
{
	m_bMouseTracking = FALSE;
	m_bHover = FALSE;
	m_bIsMouseOn  = FALSE;
	m_bIsLBtnDown = FALSE;
	m_bIsSendMessage = FALSE;
}

CSnightButton::~CSnightButton(void)
{
}

BEGIN_MESSAGE_MAP(CSnightButton, CEdit)
	ON_MESSAGE(WM_MOUSELEAVE,OnMouseLeave)
 	ON_WM_ERASEBKGND()
 	ON_WM_LBUTTONDOWN()
 	ON_WM_LBUTTONUP()
 	ON_WM_MOUSEMOVE()
 	ON_WM_DESTROY()
	ON_WM_NCHITTEST()
 	ON_WM_NCLBUTTONUP()
	ON_WM_CREATE()
	ON_WM_PAINT()
END_MESSAGE_MAP()

BOOL CSnightButton::Create(RECT& rect, CWnd* pParentWnd, UINT nID)
{
	CString strMyClass;
	strMyClass = AfxRegisterWndClass(  CS_VREDRAW | CS_HREDRAW,  ::LoadCursor(NULL, IDC_ARROW),  (HBRUSH) ::GetStockObject(WHITE_BRUSH), ::LoadIcon(NULL, IDI_APPLICATION));
	if (!CWnd::Create(strMyClass, NULL, WS_CHILD|WS_VISIBLE, rect, pParentWnd,nID ))
		return FALSE;
	return TRUE;
}

int CSnightButton::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (__super::OnCreate(lpCreateStruct) == -1)
		return -1;

	CSNBasePaintWnd::Init(m_hWnd);
	m_oNumal =  ResousContenerInstance->GetBitmapByPathName(_T("ctrl\\button\\ButtonNumal.bmp"));
	m_oMouseOn = ResousContenerInstance->GetBitmapByPathName(_T("ctrl\\button\\ButtonMouseOn.bmp"));
	m_oMouseClick =  ResousContenerInstance->GetBitmapByPathName(_T("ctrl\\button\\ButtonClick.bmp"));
	return 0;
}

void CSnightButton::OnDestroy()
{
	__super::OnDestroy();

	CSNBasePaintWnd::UnInit(m_hWnd);
}

void CSnightButton::OnPaint()
{
	RECT rcPaint = {0};
	if( !::GetUpdateRect(m_hWnd, &rcPaint, FALSE) ) 
		return ;

	RECT loClientRect = {0};
	::GetClientRect(m_hWnd, &loClientRect);

	if( !::IsRectEmpty(&loClientRect) ) 
	{
		if( m_hDcOffscreen != NULL ) 		::DeleteDC(m_hDcOffscreen);
		if( m_hbmpOffscreen != NULL )		::DeleteObject(m_hbmpOffscreen);
		m_hDcOffscreen = NULL;
		m_hbmpOffscreen = NULL;	
	}

	if(!m_hbmpOffscreen)
	{
		m_hDcOffscreen = ::CreateCompatibleDC(m_hDcPaint);
		m_hbmpOffscreen = ::CreateCompatibleBitmap(m_hDcPaint, 
			(loClientRect.right - loClientRect.left), (loClientRect.bottom - loClientRect.top)); 
	}

	PAINTSTRUCT ps;
	::BeginPaint(m_hWnd, &ps);

	HBITMAP hOldBitmap = (HBITMAP) ::SelectObject(m_hDcOffscreen, m_hbmpOffscreen);
	int iSaveDC = ::SaveDC(m_hDcOffscreen);
	::SetBkColor(m_hDcOffscreen,RGB(255, 0, 255));

	CSNBasePaintWnd::DrawParentWndBg(m_hWnd, m_hDcOffscreen);

	RECT loPicRect = { 0, 0, 86, 30,};
	RECT LoCor    = {0}; 
	RECT loPicCut = {0}; 
	if (m_bIsLBtnDown)
		CSNRander::DrawBitmapTBlt(m_hDcOffscreen, m_oMouseClick, loClientRect, loClientRect, loPicRect, LoCor);
	else if (m_bIsMouseOn)
		CSNRander::DrawBitmapTBlt(m_hDcOffscreen, m_oMouseOn, loClientRect, loClientRect, loPicRect, LoCor);
	else
		CSNRander::DrawBitmapTBlt(m_hDcOffscreen, m_oNumal, loClientRect, loClientRect, loPicRect, LoCor);

	CSNRander::DrawText(m_hDcOffscreen, m_sContext, DEF_TEXT_NUMAL_COLOR, loClientRect,  18, 6,TRUE);

	::RestoreDC(m_hDcOffscreen, iSaveDC);
	::TransparentBlt(	ps.hdc, ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right - ps.rcPaint.left,
		ps.rcPaint.bottom - ps.rcPaint.top, m_hDcOffscreen, ps.rcPaint.left, ps.rcPaint.top, (ps.rcPaint.right - ps.rcPaint.left), (ps.rcPaint.bottom - ps.rcPaint.top),
		RGB(255,0,255));
	::EndPaint(m_hWnd, &ps);
}

BOOL CSnightButton::OnEraseBkgnd(CDC* pDC)
{
	return FALSE;
}

void CSnightButton::OnLButtonDown(UINT nFlags, CPoint point)
{

	SetCapture();
	m_bIsLBtnDown = TRUE;
	m_bIsSendMessage = TRUE;
	RedrawWindow(NULL,NULL,RDW_FRAME|RDW_INVALIDATE|RDW_ERASE|RDW_ERASENOW);

	__super::OnLButtonDown(nFlags, point);
}

void CSnightButton::OnLButtonUp(UINT nFlags, CPoint point)
{

	ReleaseCapture();
	m_bIsLBtnDown =FALSE;
	RedrawWindow(NULL,NULL,RDW_FRAME|RDW_INVALIDATE|RDW_ERASE|RDW_ERASENOW);

	if (m_bIsSendMessage)
	{
		CWnd* lhParent = GetParent();
		if (lhParent && IsWindow(lhParent->m_hWnd))
			::PostMessage(lhParent->m_hWnd, MY_WM_ON_BUTTON_CTRL_DOWN, m_iCtrlId, 0);
	}
	__super::OnLButtonUp(nFlags, point);
}

void CSnightButton::OnMouseMove(UINT nFlags, CPoint point)
{
	m_bIsMouseOn = TRUE;
	if (!m_bMouseTracking)
	{
		TrackMouseLeave(GetSafeHwnd());

		m_bMouseTracking = TRUE;
		m_bHover = TRUE;
		RedrawWindow(NULL,NULL,RDW_FRAME|RDW_INVALIDATE|RDW_ERASE|RDW_ERASENOW);
	}

	RECT loRect;
	GetClientRect(&loRect);
	if (!PtInRect(&loRect, point))
	{
		m_bIsSendMessage =FALSE;
	}

	__super::OnMouseMove(nFlags, point);
}

LRESULT CSnightButton::OnMouseLeave( WPARAM wparam, LPARAM lparam )
{
	m_bMouseTracking = FALSE;
	m_bHover = FALSE;
	m_bIsMouseOn = FALSE;
	RedrawWindow(NULL,NULL,RDW_FRAME|RDW_INVALIDATE|RDW_ERASE|RDW_ERASENOW);

	return TRUE;
}




